/*
 * Copyright (c) 2011 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.snj.visangedu.calendar;

import java.io.IOException;

import android.os.AsyncTask;

import com.google.api.client.http.HttpHeaders;
import com.google.api.client.json.JsonFactory;
import com.google.api.services.calendar.model.CalendarList;
import com.google.api.services.calendar.model.CalendarListEntry;
import com.snj.visangedu.studytime.StudyPlannerFragment;

/**
 * Asynchronously load the calendars with a progress dialog.
 *
 * @author Ravi Mistry
 */
public class GC_Update_Calendar extends AsyncTask<Void, Void, Void> {

  private boolean bReturn = true;

  GC_Update_Calendar() {
  }

  @Override
  protected void onPreExecute() {
  }

  @Override
  protected Void doInBackground(Void... arg0) {
    try {
    	StudyPlannerFragment.Instance.calendars.clear();
      com.google.api.services.calendar.Calendar.CalendarList.List list =
    		  StudyPlannerFragment.Instance.client.calendarList().list();

      list.setFields("items");

      CalendarList feed = list.execute();

      if (feed.getItems() != null) {
        for (CalendarListEntry calendar : feed.getItems()) {
          // calendar.
          CalendarInfo info = new CalendarInfo(calendar.getId(), calendar.getSummary());

          info.szAccessRole = calendar.getAccessRole();
          info.szColorId = calendar.getColorId();
          info.szDescription = calendar.getDescription();
          info.szEtag = calendar.getEtag();
          info.szKind = calendar.getKind();
          info.szLocation = calendar.getLocation();
          info.szSummaryOverride = calendar.getSummaryOverride();
          info.szTimeZone = calendar.getTimeZone();

          // EventReminder getResponseHeaders
          HttpHeaders header = calendar.getResponseHeaders();
          if (header != null)
            info.szResponseHeaders = header.toString();
          else
            info.szResponseHeaders = "";

          // EventReminder JsonFactory
          JsonFactory jf = calendar.getFactory();
          if (jf != null)
            info.szFactory = jf.toString();
          else
            info.szFactory = "";

          StudyPlannerFragment.Instance.calendars.add(info);
        }
      }
    } catch (IOException e) {
      bReturn = false;
    } catch (Exception e) {
      bReturn = false;
    } finally {
    }
    return null;
  }

  @Override
  protected void onPostExecute(Void result) {
    AsyncManager.Instance.UpdateWorkCalendarComplete(bReturn);
  }
}
